package com.tongzhan.filter;

import com.tongzhan.util.JwtUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;


/**
 * @title: AuthorizeFilter
 * @projectName: xm
 * @description: TODO
 * @author: Zack_Tzh
 * @date: 2019/12/10  14:46
 */
@Component
public class AuthorizeFilter implements GlobalFilter, Ordered  {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //1. 获取请求
        ServerHttpRequest request = exchange.getRequest();
        //2. 则获取响应
        ServerHttpResponse response = exchange.getResponse();
        //3. 如果是登录请求则放行
        if (request.getURI().getPath().contains("/login")) {
            return chain.filter(exchange);
        }
        if (request.getURI().getPath().contains("/manageLogin")) {
            return chain.filter(exchange);
        }
        if (request.getURI().getPath().contains("/getTest")) {
            return chain.filter(exchange);
        }
        if (request.getURI().getPath().contains("/screen")) {
            return chain.filter(exchange);
        }
        if (request.getURI().getPath().contains("/upload")) {
            return chain.filter(exchange);
        }
        //4. 获取请求头
        HttpHeaders headers = request.getHeaders();
        //5. 请求头中获取令牌
        String authorization = headers.getFirst("Authorization");
        //6. 判断请求头中是否有令牌
        if (StringUtils.isEmpty(authorization) || "".equals(authorization.trim())) {
            Mono<Void> result = Result.result(response, "300", "### 用户未登录，请先登录 ###", "");
            return result;
        }
        //9. 如果请求头中有令牌则解析令牌
        try {
            //10. 获取token
            String token;
            if (authorization.startsWith("Bearer")) {
                token = authorization.substring(7);
            } else {
                token = authorization;
            }
            JwtUtil.parseJWT(token);
//            if(claims != null) {
//                String apis  = (String)claims.get("apis");
//                if(apis.contains(request.getURI().getPath())){
//                    return chain.filter(exchange);
//                } else {
//                    Mono<Void> result = Result.result(response, "401", "此用户无权访问此接口", "");
//                    return result;
//                }
//            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.result(response, "401", "此token不合法", "");
        }
        //12. 放行
        return chain.filter(exchange);
    }


    @Override
    public int getOrder() {
        return 0;
    }
}